package com.onemore.task.remote;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.voovan.http.server.HttpResponse;
import org.voovan.tools.log.Logger;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.onemore.task.OneJdbc;
import com.onemore.task.model.ModelShangpin;
import com.onemore.task.model.ModelShangpin_A;
import com.onemore.task.model.ModelSpgg1;
import com.onemore.task.model.ModelSpgg2;
import com.sun.tools.javac.util.Log;

public class TaskGoods extends RmtTaskBase{
	public void process(JSONObject jsonObject,HttpResponse response)
	{
    	Date dt = new Date();     
		JSONObject goodsJson = jsonObject;
		JSONObject goodsJson_A = (JSONObject)goodsJson.clone();
		JSONArray spgg1Array = goodsJson.getJSONArray("ITEM_SPGG1");
		JSONArray spgg2Array = goodsJson.getJSONArray("ITEM_SPGG2");
		int ret = 0;
		outer:do {
			if(spgg1Array == null) {
				message = "接收到的商品添加报文，没有找到ITEM_SPGG1字段";
	    		Logger.error(message);
				ret = -520;
				break outer;
	    	}
			if(spgg2Array == null) {
				message = "接收到的商品添加报文，没有找到ITEM_SPGG2字段";
	    		Logger.error(message);
				ret = -521;
				break outer;
	    	}

			String djbh = goodsJson.getString(ModelShangpin.goods_field[0]);
			for(int j=0;j<spgg1Array.size();j++) {
				String mxdjbh = spgg1Array.getJSONObject(j).getString(ModelSpgg1.spgg1_field[0]);
				if(!mxdjbh.startsWith(djbh)) {
					message = "明细表单据编号跟主表单据编号不统一";
					Logger.error(message);
					ret = -500;
					break outer;
				}
			}
			for(int j=0;j<spgg2Array.size();j++) {
				String mxdjbh = spgg2Array.getJSONObject(j).getString(ModelSpgg2.spgg2_field[0]);
				if(!mxdjbh.startsWith(djbh)) {
					message = "明细表单据编号跟主表单据编号不统一";
					Logger.error(message);
					ret = -501;
					break outer;
				}
			}

			//规格有效性校验
			for(int j=0;j<spgg1Array.size();j++) {
				JSONObject json = spgg1Array.getJSONObject(j);
		    	String guige = json.getString(ModelSpgg1.spgg1_field[1]);
		    	if(guige == null) {
					message = "请求数据错误，规格1数据未填写！";
		    		Logger.error(message);
					ret = -510;
					break outer;
		    	}
		    	try {
					if(!OneJdbc.seach_guige1(guige)) {
						message = "请求的规格1数据未找到！";
						Logger.error(message);
						ret = -511;
						break outer;
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					message = e.getMessage();
					ret = -212;
					Logger.error(e);
					break outer;
				}
			}
			for(int j=0;j<spgg2Array.size();j++) {
				JSONObject json = spgg2Array.getJSONObject(j);
		    	String guige = json.getString(ModelSpgg2.spgg2_field[1]);
		    	if(guige == null) {
					message = "请求数据错误，规格2数据未填写！";
		    		Logger.error(message);
					ret = -512;
					break outer;
		    	}
		    	try {
					if(!OneJdbc.seach_guige2(guige)) {
						message = "请求的规格2数据未找到！";
						Logger.error(message);
						ret = -513;
						break outer;
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					message = e.getMessage();
					ret = -212;
					Logger.error(e);
					break outer;
				}
			}
	    	setDefault(goodsJson,"ONLINE","1");//入库类型
	    	setDefault(goodsJson,"DWMC","000");//
	    	setDefault(goodsJson,"FJSX1","000");//
	    	setDefault(goodsJson,"FJSX2","000");//
	    	setDefault(goodsJson,"FJSX3","000");//
	    	setDefault(goodsJson,"FJSX4","000");//
	    	setDefault(goodsJson,"FJSX5","000");//
	    	setDefault(goodsJson,"FJSX6","000");//
	    	setDefault(goodsJson,"FJSX7","000");//
	    	setDefault(goodsJson,"FJSX8","000");//
	    	setDefault(goodsJson,"FJSX9","000");//
	    	setDefault(goodsJson,"FJSX10","000");//
	    	setDefault(goodsJson,"FJSX11","000");//
	    	setDefault(goodsJson,"FJSX12","000");//
	    	setDefault(goodsJson,"FJSX13","000");//
	    	setDefault(goodsJson,"FJSX14","000");//
	    	setDefault(goodsJson,"FJSX15","000");//
	    	setDefault(goodsJson,"FJSX16","000");//
	    	setDefault(goodsJson,"BYZD18","简派SCM");//
	    	setDefault(goodsJson,"BYZD1","0");//
	    	setDefault(goodsJson,"BYZD2","2");//
	    	setDefault(goodsJson,"BYZD3","7");
	    	setDefault(goodsJson,"BYZD4","1");//
	    	setDefault(goodsJson,"BYZD5","000");//
	    	setDefault(goodsJson,"BYZD6","");//
	    	setDefault(goodsJson,"BYZD7","");//
	    	setDefault(goodsJson,"BYZD8","000");//
	    	setDefault(goodsJson,"BZJJ","0");//
	    	setDefault(goodsJson,"BYZD14",sdf.format(dt));//
	    	setDefault(goodsJson,"BYZD15",sdf.format(dt));//
	    	goodsJson.put("TZSY", "1");
	    	goodsJson_A.put("BYZD3", "简派SCM");
	    	goodsJson_A.put("BYZD4", "简派SCM");
	    	setDefault(goodsJson_A,"BYZD9","0");//
			//插入商品表
			for(int i=1;i<11;i++) {
				try {
					if(OneJdbc.insert_goods(goodsJson) ) {
						break;
					}else {
						message = "插入商品失败,错误计数："+i;
						Logger.error(message);
						if(i == 1) {
							ret = -101;
							break outer;								
						}
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					message = e.getMessage();
					ret = -213;
					Logger.error(e);
					break outer;		
				}
			}
			//插入商品附表
			for(int i=1;i<11;i++) {
				try {
					if(ModelShangpin_A.insert(goodsJson_A) ) {
						break;
					}else {
						message = "插入商品附表失败,错误计数："+i;
						Logger.error(message);
						if(i == 1) {
							ret = -101;
							break outer;								
						}
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					message = e.getMessage();
					ret = -213;
					Logger.error(e);
					break outer;		
				}
			}
			for(int j=0;j<spgg1Array.size();j++) {
				JSONObject spgg1 = spgg1Array.getJSONObject(j);
    			for(int i=1;i < 11;i++) {
    				try {
						if(OneJdbc.insert_spgg1(spgg1)) {
							break;
						}else {
							message = "插入规格1失败,错误计数："+i;
							Logger.error(message);

							if(i == 1) {
								ret = -102;
								break outer;	    								
							}
						}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						message = e.getMessage();
						ret = -215;
						Logger.error(e);
						break outer;
					}
    			}
			}
			for(int j=0;j<spgg2Array.size();j++) {
				JSONObject spgg2 = spgg2Array.getJSONObject(j);
    			for(int i=1;i<11;i++) {
    				try {
						if(OneJdbc.insert_spgg2(spgg2)) {
							break;
						}else {
							message = "插入规格2失败,错误计数："+i;
							Logger.error(message);
							if(i == 1) {
								ret = -103;
								break outer;	    								
							}
						}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						message = e.getMessage();
						ret = -216;
						Logger.error(e);

						break outer;
					}
    			}
			}
		}while(false);
		if(ret == 0 || ret == 200) {
			Logger.info("成功插入从SCM获取商品的数据，ret:"+ret+" message:"+message);
		}
		putResult(response,ret,message);
	}
}
